package com.sihai.ehr.service.impl;

import com.sihai.core.*;
import com.sihai.ehr.model.biz.FinancialReportForm;
import com.sihai.ehr.model.biz.FinancialReportFormAuditing;
import com.sihai.ehr.model.biz.FinancialReportFormDetail;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.repository.AuditingRepository;
import com.sihai.ehr.service.FinancialReportFormService;
import com.sihai.ehr.web.pojo.SearchFormPojo;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * author: wxn
 * since: 13-4-14 下午12:18
 */
@Service("financialReportFormService")
public class FinancialReportFormServiceImpl implements FinancialReportFormService {
    @Resource
    private DataAccessor dataAccessor;
    @Resource
    private AuditingRepository auditingRepository;

    @Override
    public void save(FinancialReportForm financialReportForm, List<FinancialReportFormDetail> details) throws Exception {
        Assert.notNull(financialReportForm, "financialReportForm is required");
        boolean isCreate = (financialReportForm.getId() == null || financialReportForm.getId() <= 0);
        dataAccessor.save(financialReportForm);
        if(details != null){
            for(FinancialReportFormDetail detail : details){
                detail.setReportForm(financialReportForm);
                dataAccessor.save(detail);
            }
        }
        if(isCreate){
            saveFinancialReportFormAuditing(financialReportForm, 1);
        }
    }

    @Override
    public FinancialReportForm get(Long id) throws Exception {
        Assert.notNull(id, "financialReportForm id is required");
        return dataAccessor.get(FinancialReportForm.class, id);
    }

    @Override
    public boolean delete(String ids) throws Exception {
        final String deleteSql = String.format("delete from FinancialReportForm frt where frt.id in (%s)", ids);
        this.dataAccessor.batchUpdate(new QueryCreator() {
            @Override
            public Query createQuery(Session session) {
                return session.createQuery(deleteSql);
            }
        });
        return true;
    }

    @Override
    public LimitedList<FinancialReportForm> findFinancialReportForm(User user, final SearchFormPojo searchFormPojo, Limit limit, Sorter sorter) throws Exception {
        if(user == null || user.getWork() == null){
            if(searchFormPojo == null){
                return dataAccessor.findByHql(FinancialReportForm.class, "from FinancialReportForm", null, limit, sorter);
            }
            return dataAccessor.findByQuery(FinancialReportForm.class, new QueryCreator(){
                @Override
                public Query createQuery(Session session) {
                    DynamicQuery query = new DynamicQuery("from FinancialReportForm f where 1=1");
                    query.appendIfNotNull("and f.create_date > :startDate","startDate", searchFormPojo.getStartDate());
                    query.appendIfNotNull("and f.create_date < :endDate","endDate", searchFormPojo.getEndDate());
                    //query.appendIfNotNull("and f.work.organization.id = :organizationId", "organizationId", searchFormPojo.getOrgId());
                    if(searchFormPojo.getOrgId() != null && searchFormPojo.getOrgId() > 0){
                        Organization o = dataAccessor.get(Organization.class, searchFormPojo.getOrgId());
                        if(o != null){
                            query.append("and f.work.organization.treePath like '" + o.getTreePath() + "%'");
                        }
                    }
                    query.appendIfNotNull("and f.work.duty.id = :dutyId", "dutyId", searchFormPojo.getDutyId());
                    return query.build(session);
                }
            }, limit, sorter);
        }
        return dataAccessor.findByHql(FinancialReportForm.class, "from FinancialReportForm f where f.work.id=?", new Object[]{user.getWork().getId()}, limit, sorter);
    }

    //查询财务报支单审核流程记录
    @Override
    public List<List<FinancialReportFormAuditing>> findFinancialReportFormAuditing(Long financialReportFormId) throws Exception {
        return auditingRepository.findByHql(FinancialReportFormAuditing.class, "from FinancialReportFormAuditing ta where ta.financialReportForm.id = ?", new Object[]{financialReportFormId});
    }

    //保存财务报支单审核流程记录
    @Override
    public void saveFinancialReportFormAuditingProcess(Long financialReportFormId) {
        saveFinancialReportFormAuditing(dataAccessor.get(FinancialReportForm.class, financialReportFormId), 1);
    }

    //创建审核流程
    private void saveFinancialReportFormAuditing(FinancialReportForm financialReportForm, int auditNo) {
        auditingRepository.saveAuditingProcess(financialReportForm, auditNo);
    }

    @Override
    public List<FinancialReportFormDetail> findFinancialReportFormDetails(FinancialReportForm financialReportForm) {
        Assert.notNull(financialReportForm, "financialReportForm is required");
        return dataAccessor.findByHql(FinancialReportFormDetail.class, "from FinancialReportFormDetail d where d.reportForm.id=?", new Object[]{financialReportForm.getId()}, null, null);
    }
}
